package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"

	//"api-gateway/basic/config"
	//__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"

	"net/http"
)

func ShopwinAdd(c *gin.Context) {
	var e request.ShopwindowAdd
	if err := c.ShouldBind(&e); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	r, err := config.ShopClient.ShopwindowAdd(config.Ctx, &__.ShopwindowAddReq{
		Name:                 e.Name,
		Stock:                int64(e.Stock),
		Price:                float32(e.Price),
		UserId:               int64(e.UserId),
		ShopwindowCargetryId: int64(e.ShopwindowCargetryId),
		Image:                e.Image,
		Status:               int64(e.Status)})

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": r.Id,
	})
}

func ShopAdd(c *gin.Context) {
	var e request.ShopAdd
	if err := c.ShouldBind(&e); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	r, err := config.ShopClient.ShopAdd(config.Ctx, &__.ShopAddReq{
		Name:                 e.Name,
		Num:                  int64(e.Num),
		Price:                float32(e.Price),
		UserId:               int64(e.UserId),
		ShopwindowCargetryId: int64(e.ShopwindowCargetryId),
		Image:                e.Image,
		Status:               int64(e.Status)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	if e.Name != "" && e.Num > 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "名字不为空,库存是非负数",
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "商品橱窗添加成功",
		"data": r.Id,
	})
}
